package cn.lg.soar.redis.config;

import cn.lg.soar.core.config.cache.ICacheManagerSupplier;
import cn.lg.soar.core.config.cache.template.ICacheTemplate;
import cn.lg.soar.core.manager.ITokenStateManager;
import cn.lg.soar.redis.cache.CacheManagerRedisSupplier;
import cn.lg.soar.redis.cache.template.CacheTemplateRedis;
import cn.lg.soar.redis.cache.template.CacheTemplateRedisByteArray;
import cn.lg.soar.redis.cache.template.CacheTemplateRedisString;
import cn.lg.soar.redis.manager.RedisTokenStateManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;

/**
 * redis配置类
 * @Author: lgx
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
@Configuration
@ConditionalOnProperty(value = "lg.redis.enabled", havingValue = "true", matchIfMissing = true)
public class CacheConfig {

    @Autowired
    private RedisTemplate<String, Object> redis;
    @Autowired
    private RedisTemplate<String, byte[]> byteRedisTemplate;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private RedisTemplate<String, Long> longRedisTemplate;

    @Primary
    @Bean
    public ICacheTemplate<String, Object> cacheTemplate(){
        return new CacheTemplateRedis(redis);
    }

    @Primary
    @Bean
    public ICacheTemplate<String, byte[]> byteArrayCacheTemplate(){
        return new CacheTemplateRedisByteArray(byteRedisTemplate);
    }

    @Primary
    @Bean
    public ICacheTemplate<String, String> stringCacheTemplate(){
        return new CacheTemplateRedisString(stringRedisTemplate);
    }

    @Primary
    @Bean
    public ICacheManagerSupplier cacheManagerSupplier(){
        return new CacheManagerRedisSupplier();
    }

    @Primary
    @Bean
    public ITokenStateManager redisTokenStateManager() {
        return new RedisTokenStateManager(longRedisTemplate);
    }
}
